﻿using System;
using Autodesk.Revit.DB;
using Transfer.Utils;

namespace Transfer.CommonUnit
{
	public class Locator
	{
		public static FindCurveInfo GetNewPosition(Curve curve, Transform transform, bool isMirrored)
		{
			return Locator.GetNewPosition(new FindCurveInfo(curve), transform, isMirrored);
		}

		public static XYZ GetNewPosition(XYZ ptLocation, Transform transform)
		{
			XYZ xyz = Geometry.TransformPoint(ptLocation, transform);
			return new XYZ(xyz.X, xyz.Y, 0.0);
		}

		public static XYZ GetNewDirection(XYZ ptDirection, Transform transform)
		{
			XYZ newPosition = Locator.GetNewPosition(XYZ.Zero, transform);
			return Locator.GetNewPosition(ptDirection, transform) - newPosition;
		}

		private static FindCurveInfo GetNewPosition(FindCurveInfo curveInfo, Transform transform, bool isMirrored)
		{
			XYZ xyz = Geometry.TransformPoint(curveInfo.StartPoint, transform);
			XYZ xyz2 = Geometry.TransformPoint(curveInfo.EndPoint, transform);
			xyz = new XYZ(xyz.X, xyz.Y, 0.0);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
			double bulge = 0.0;
			FindCurveInfo result;
			if (curveInfo.IsArc)
			{
				XYZ xyz3 = null;
				Geometry.GetCenterWithBulge(curveInfo.StartPoint, curveInfo.EndPoint, curveInfo.Bulge, ref xyz3);
				xyz3 = Geometry.TransformPoint(xyz3, transform);
				if (isMirrored)
				{
					bulge = Geometry.GetBulge(xyz2, xyz, xyz3, true);
					result = new FindCurveInfo(xyz2, xyz, bulge);
				}
				else
				{
					bulge = Geometry.GetBulge(xyz, xyz2, xyz3, true);
					result = new FindCurveInfo(xyz, xyz2, bulge);
				}
			}
			else
			{
				result = new FindCurveInfo(xyz, xyz2, bulge);
			}
			return result;
		}
	}
}
